package com.platform.os.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 * @author KangJun 2007-9-26.
 */
@SuppressWarnings("unchecked")
public class XmlHandle {
	/** 获得节点文本值.
	 * @param nodeName 格式为xpath格式：/util/parameter/customer
	 * @return String
	 */
	public static String getNode(String realFile, String nodeName) {
		try {
			File file = new File(realFile);
			SAXReader reader = new SAXReader();
			Document document = (Document) reader.read(file);
			Node node = document.selectSingleNode(nodeName);
			return node.getText();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/** 设置节点文本值.
	 * @param nodeName 格式为xpath格式：/util/parameter/customer
	 * @param textValue 文本值
	 * @return boolean
	 */
	public static boolean setNode(String realFile, String nodeName, String textValue) {
		try {
			File file = new File(realFile);
			SAXReader reader = new SAXReader();
			Document document = reader.read(file);
			Node node = document.selectSingleNode(nodeName);
			node.setText(textValue);

			return writeFile(realFile, document);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/** 增加节点.
	 * @param nodeName 格式为xpath格式：/util/parameter/customer
	 * @param textValue 文本值
	 * @return boolean
	 */
	public static boolean addNode(String realFile, String nodeName, String textValue) {
		int index = nodeName.lastIndexOf('/');
		String parentName = nodeName.substring(0, index);
		String newNode = nodeName.substring(index + 1);

		try {
			File file = new File(realFile);
			SAXReader reader = new SAXReader();
			Document document = reader.read(file);

			List list = document.selectNodes(parentName);
			Iterator iter = list.iterator();
			if (iter.hasNext()) {
				Element parentElement = (Element) iter.next();
				Element childElement = parentElement.addElement(newNode);
				childElement.setText(textValue);
			}

			return writeFile(realFile, document);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/** 删除节点.
	 * @param nodeName 格式为xpath格式：/util/parameter/customer
	 * @return boolean
	 */
	public static boolean removeNode(String realFile, String nodeName) {
		try {
			File file = new File(realFile);
			SAXReader reader = new SAXReader();
			Document document = reader.read(file);
			Node node = document.selectSingleNode(nodeName);
			node.detach();

			return writeFile(realFile, document);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/** XML文件写入.
	 * @param file 全路径文件名.
	 * @param document
	 * @return
	 */
	private static boolean writeFile(String file, Document document) {
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("UTF-8");
		format.setTrimText(true);
		try {
			XMLWriter writer = new XMLWriter(new FileOutputStream(file), format);
			writer.write(document);
			writer.close();
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}
}
